close all;
clear;

%% 
% product_name = 'LT1_143448';
% product_name = 'GF3_178100';
product_name = 'LT1_210226';

%% 读取 DEM
dem_path = ['pipe01_simsar/results/', product_name, '/', product_name, '_resamp.dem'];
dem_par_path = ['pipe01_simsar/results/', product_name, '/', product_name, '_resamp.dem_par'];

[dem_data, dem_param] = gamma.readDem(dem_path, dem_par_path);

figure;
imagesc(dem_data);
axis equal;
axis tight;
title('DEM');

%% 读取 SAR
mli_path = ['pipe01_simsar/results/', product_name, '/', product_name  , '.mli'];
mli_par_path = ['pipe01_simsar/results/', product_name, '/', product_name  , '.mli_par'];

mli_data = gamma.readMli(mli_path, mli_par_path);

% tmp = hsUtils.stretchImageByHist(mli_data, 10000, "HistHalfPerc", 0.025);
% tmp = single(tmp);
tmp = mli_data;
thre = mean(tmp(:)) * 3;
tmp(tmp > thre) = thre;

figure;
imagesc(tmp);
axis equal;
axis tight;
title('multilook SAR');

%% 读取地理编码表
lkt_path = ['pipe01_simsar/results/', product_name, '/', product_name  , '.lkt'];

[rid_grid, aid_grid] = gamma.readLKT(lkt_path, dem_par_path);
[Nr, Na] = gamma.getMliSize(mli_par_path);

% mask = (rid_grid >= 0 & rid_grid < Nr & aid_grid >= 0 & aid_grid < Na);
% rid_grid(~mask) = nan;
% aid_grid(~mask) = nan;

figure;
subplot(121);
imagesc(rid_grid);
title('range');
axis equal;
axis tight;
subplot(122);
imagesc(aid_grid);
title('azimuth');
axis equal;
axis tight;

%% 读取叠掩和阴影掩膜
ls_eqa_path = ['pipe01_simsar/results/', product_name, '/', product_name  , '.ls_eqa'];
ls_eqa_mask = gamma.readLsEqaMask(ls_eqa_path, dem_par_path);

ls_rdc_path = ['pipe01_simsar/results/', product_name, '/', product_name  , '.ls_rdc'];
ls_rdc_mask = gamma.readLsRdcMask(ls_rdc_path, mli_par_path);

figure;
subplot(121);
imagesc(ls_eqa_mask);
title('ls EQA');
axis equal;
axis tight;
subplot(122);
imagesc(ls_rdc_mask);
title('ls RDC');
axis equal;
axis tight;

%% 读取模拟SAR影像
simsar_eqa_path = ['pipe01_simsar/results/', product_name, '/', product_name  , '.simsar_eqa'];
simsar_eqa = gamma.readSimsarEqa(simsar_eqa_path, dem_par_path);

simsar_rdc_path = ['pipe01_simsar/results/', product_name, '/', product_name  , '.simsar_rdc'];
simsar_rdc = gamma.readSimsarRdc(simsar_rdc_path, mli_par_path);

figure;
% subplot(121);
% imagesc(simsar_eqa);
% title('simsar EQA');
% axis equal;
% axis tight;
% subplot(122);
imagesc(simsar_rdc);
title('simsar RDC');
axis equal;
axis tight;

%%
[offset_x, offset_y, response, max_rc] = hs.img.match.estOffsetByCC(mli_data, simsar_rdc, "ReferPadRadiusX", 1500, "ReferPadRadiusY", 1500, "EnableNorm", false);

figure;
imagesc(response);
hold on;
plot(max_rc(2), max_rc(1), 'rx');

% figure; mesh(response)

